﻿package smooth.core.object.flickrUserComponent {
	
	import app.core.action.RotatableScalable;
	import flash.events.MouseEvent;
	import flash.events.TouchEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.AntiAliasType;
	import flash.display.Shape;
	import smooth.core.object.flickrComponent.FlickrComponent;
	import caurina.transitions.Tweener;	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.net.URLRequest;
	
	public class FlickrUserComponent extends RotatableScalable {
		
		[Embed(source="Arial.ttf", fontFamily="myFont")] 
		public var myFont:Class;
		
		private var flickrUserObject:FlickrUserObject;
		private var clickgrabber:Shape = new Shape();	
		public var initialPosX:int;
		public var initialPosY:int;
		public var t:TextField;
				
		public function FlickrUserComponent(flickrUserObject:FlickrUserObject) {
			this.flickrUserObject = flickrUserObject;
			// Init des visuels du composant			
			initGraphics();
			// Init des events du composant
			initEvents();	
			// Init des process du composant
			initProcess();
		}
		
		
		private function initGraphics():void {
			if (this.flickrUserObject != null) {
				clickgrabber.graphics.lineStyle(4,0xFFFFFF, 1);
				clickgrabber.graphics.beginFill(0xFFFFFF, 0.5);
				clickgrabber.graphics.drawCircle(0, 0, 45);
				clickgrabber.graphics.endFill();
				this.addChild(clickgrabber);
			
				var format:TextFormat= new TextFormat();
				format.font= "myFont";
				format.color= 0xFFFFFF;
				format.size = 30;
				
				this.t = new TextField();
				this.t.background = false;	
				this.t.alpha = 1;
				this.t.embedFonts = true;
				this.t.selectable  = false;		 
				this.t.text = this.flickrUserObject.getUsername();
				this.t.autoSize = TextFieldAutoSize.LEFT;
				this.t.name = this.flickrUserObject.getNsid();
				this.t.x = 50;
				this.t.y = -15;
				this.t.setTextFormat(format);
				this.addChild(this.t);
				
				var imageLoader:Loader =new Loader();
				imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,userIconLoadComplete);
				imageLoader.load(new URLRequest("../resources/user.png"));
			}
		}
		
		private function initProcess():void {
		}
		
		private function initEvents():void {
			this.addEventListener(TouchEvent.MOUSE_UP, this.doTouchUp);
			this.addEventListener(MouseEvent.MOUSE_UP, this.doMouseUp);
		}
		
		private function userIconLoadComplete(event:Event):void {
			var userIcon:Bitmap = new Bitmap();
			userIcon.bitmapData = event.target.content.bitmapData;
			userIcon.alpha = 0.5;
			userIcon.width = 80;
			userIcon.height = 80;
			userIcon.x = -40;
			userIcon.y = -40;
			userIcon.alpha = 0.8;
			this.addChild(userIcon);			
		}
		
		private function doMouseUp(e:MouseEvent):void {
			var point:Point = new Point(e.stageX, e.stageY);
			doProcessMouseUp(point);
		}
		
		private function doTouchUp(e:TouchEvent):void {
			var point:Point = new Point(e.stageX, e.stageY);
			doProcessMouseUp(point);
		}
  
		private function doProcessMouseUp(point:Point):void {
			var actionsToProcess:Array = new Array();
			var targetObjects:Array = this.stage.getObjectsUnderPoint(point);
			actionsToProcess.unshift({objectType:"", targetObject:null})
			for (var i:int = 0; i < targetObjects.length;++i) {
				var targetObject:Object = targetObjects[i];
				if (targetObject is FlickrComponent) {
					actionsToProcess.unshift({objectType:"FlickrComponent", targetObject:targetObject});
				}
			}
			
			if (actionsToProcess[0].objectType == "FlickrComponent") {
				actionsToProcess[0].targetObject.userPhotosSearchService(this);
				Tweener.addTween(this, { alpha:0, scaleX: 0.0, scaleY: 0.0, time:0.25, transition:"easeinoutquad" } );	
			}
			
			Tweener.addTween(this, {alpha:1,scaleX: 1.0, scaleY: 1.0, x:this.initialPosX, y:this.initialPosY, time:0.25, transition:"easeinoutquad"});	
		}
		
		
		public function getNsid():String {
			if (this.flickrUserObject != null) {
				return this.flickrUserObject.getNsid();
			} else {
				return null;
			}
		}
		
		public function getUsername():String {
			if (this.flickrUserObject != null) {
				return this.flickrUserObject.getUsername();
			} else {
				return null;
			}
		}
		
	}
	
}